﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Logic;
using Entities;

namespace Presentation
{
	public partial class TaskDialogAddSmartlist : Form
	{
		private Smartlist smartlist = new Smartlist();

		private bool ValidateCriteria()
		{
			int r;
			if (comboBoxField.SelectedIndex != 1 && textBoxValue.Text.Trim() == "")
				return false;
			if ((comboBoxField.SelectedIndex == 3 || comboBoxField.SelectedIndex == 4) && !Int32.TryParse(textBoxValue.Text.Trim(), out r))
				return false;

			return true;
		}

		private bool ValidateData()
		{
			if (textBoxName.Text.Trim() == "")
				return false;
			if (smartlist.Criteria.Count == 0)
				return false;

			return true;
		}

		public TaskDialogAddSmartlist()
		{
			InitializeComponent();
		}

		private void TaskDialogAddSmartlist_Load(object sender, EventArgs e)
		{
			comboBoxField.DataSource = SmartlistCriteria.GetFields();
			comboBoxCondition.DataSource = SmartlistCriteria.GetConditions(comboBoxField.SelectedValue.ToString());
			comboBoxOperator.DataSource = new string[] { "AND", "OR" };

			dataListViewCriteria.ShowGroups = false;

			dataListViewCriteria.DataSource = smartlist.Criteria;
		}

		private void comboBoxField_SelectedIndexChanged(object sender, EventArgs e)
		{
			GenreBL gbl = new GenreBL();

			if (comboBoxField.SelectedValue != null)
			{
				comboBoxCondition.DataSource = SmartlistCriteria.GetConditions(comboBoxField.SelectedValue.ToString());

				if (comboBoxField.SelectedIndex == 1)
				{
					comboBoxValue.Visible = true;
					comboBoxValue.DataSource = gbl.List();
				}
				else
				{
					comboBoxValue.Visible = false;
				}
			}
		}

		private void buttonAddCriteria_Click(object sender, EventArgs e)
		{
			if (ValidateCriteria())
			{
				SmartlistCriteria criteria = new SmartlistCriteria();

				criteria.Field = comboBoxField.SelectedValue.ToString();
				criteria.Condition = comboBoxCondition.SelectedValue.ToString();
				if (comboBoxField.SelectedIndex == 1)
					criteria.Value = comboBoxValue.SelectedValue.ToString();
				else
					criteria.Value = textBoxValue.Text.Trim();
				criteria.Operator = comboBoxOperator.SelectedValue.ToString();

				smartlist.Criteria.Add(criteria);

				//comboBoxField.SelectedIndex = 0;
				textBoxValue.ResetText();
				//comboBoxOperator.SelectedIndex = 0;
			}
		}

		private void buttonRemoveCriteria_Click(object sender, EventArgs e)
		{
			if (dataListViewCriteria.SelectedObject != null)
				smartlist.Criteria.Remove((SmartlistCriteria)(dataListViewCriteria.SelectedObject));
		}

		private void dataListViewCriteria_SelectedIndexChanged(object sender, EventArgs e)
		{
			if (dataListViewCriteria.SelectedItem != null)
				buttonRemoveCriteria.Enabled = true;
			else
				buttonRemoveCriteria.Enabled = false;
		}

		private void buttonAdd_Click(object sender, EventArgs e)
		{
			if (ValidateData())
			{
				smartlist.Name = textBoxName.Text.Trim();

				SmartlistBL bl = new SmartlistBL();
				bl.SetStringCriteria(smartlist);
				if (bl.Save(smartlist))
				{
					DialogResult = DialogResult.OK;
					Close();
				}
				else
				{
					MessageBox.Show("There was an error adding the smartlist.", "Error at adding smartlist", MessageBoxButtons.OK, MessageBoxIcon.Error);
					DialogResult = DialogResult.Cancel;
				}
			}
		}
	}
}